﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WITM.BLL;
using WITM.DAL;

namespace WITMTestApplication
{
    public partial class EntriesTestForm : Form
    {
        private UserBL userBl;
        private SpendingCategoryBL spendingCategoryBl;
        private SpendingItemBL spendingItemBl;
        private SpendingEntryBL spendingEntryBl;

        public EntriesTestForm()
        {
            InitializeComponent();
            InitializeDB(false);
            InitializeItemsGrid();
            InitializeSearchResultsGrid();
            LoadUsersComboBox();
        }


        private void LoadUsersComboBox()
        {
            IEnumerable<Users> users = userBl.GetAll();
            var usersFormated = from u in users
                                select new
                                {
                                    ID = u.UserID,
                                    Name = String.Format("{0} {1}", u.FirstName, u.LastName)
                                };
            cbUsers.ValueMember = "ID";
            cbUsers.DisplayMember = "Name";
            cbUsers.DataSource = usersFormated.ToList();
        }

        private void InitializeItemsGrid()
        {
            dgvEntries.Columns.Add("colEntryID", "ID");
            dgvEntries.Columns.Add("colDescription", "Description");
            dgvEntries.Columns.Add("colDate", "Date");
            dgvEntries.Columns.Add("colPrice", "Price");
            dgvEntries.Columns.Add("colRemarks", "Remarks");
        }

        private void InitializeSearchResultsGrid()
        {
            dgvResults.Columns.Add("colItemID", "ID");
            dgvResults.Columns.Add("colCategoryDesc", "Category");
            dgvResults.Columns.Add("colItemDesc", "Item");
        }

        private void InitializeDB(bool tracking)
        {
            WhereIsTheMoneyDBEntities context = new WhereIsTheMoneyDBEntities();
            if (!tracking)
            {
                context.Users.MergeOption = System.Data.Objects.MergeOption.NoTracking;
                context.SpendingCategories.MergeOption = System.Data.Objects.MergeOption.NoTracking;
                context.SpendingItems.MergeOption = System.Data.Objects.MergeOption.NoTracking;
                context.SpendingEntries.MergeOption = System.Data.Objects.MergeOption.NoTracking;
            }
            UnitOfWork unitOfWork = new UnitOfWork(context);
            userBl = new UserBL(unitOfWork);
            spendingCategoryBl = new SpendingCategoryBL(unitOfWork);
            spendingItemBl = new SpendingItemBL(unitOfWork);
            spendingEntryBl = new SpendingEntryBL(unitOfWork);
        }

        private void LoadCategoriesComboBox()
        {
            int userID = (int)cbUsers.SelectedValue;
            IEnumerable<SpendingCategories> categories = spendingCategoryBl.GetAllPerUser(userID);
            cbCategories.ValueMember = "CategoryID";
            cbCategories.DisplayMember = "Description";
            cbCategories.DataSource = categories;
        }

        private void LoadItemsComboBox()
        {
            long categoryID = (long)cbCategories.SelectedValue;
            IEnumerable<SpendingItems> items = spendingItemBl.GetAllPerCategory(categoryID);
            cbItems.ValueMember = "ItemID";
            cbItems.DisplayMember = "Description";
            cbItems.DataSource = items;
        }

        private void cbUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCategoriesComboBox();
        }

        private void cbCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadItemsComboBox();
        }

        private void LoadSpendingEntries()
        {
            int userID = (int)cbUsers.SelectedValue;
            IEnumerable<SpendingEntries> entries = spendingEntryBl.GetAllPerDate(userID, dtpDate.Value.Date);
            dgvEntries.Rows.Clear();
            foreach (SpendingEntries entry in entries)
            {
                dgvEntries.Rows.Add(entry.EntryID, 
                    entry.SpendingItems.Description, 
                    entry.Date.ToString("yyyy-MM-dd"), 
                    entry.Price, 
                    entry.Remark);
            }
            decimal total = entries.Sum(et => et.Price);
            lblTotal.Text = String.Format("Total: {0}", total);
        }

        private void dtpDate_ValueChanged(object sender, EventArgs e)
        {
            LoadSpendingEntries();
        }

        private void btnAddEntry_Click(object sender, EventArgs e)
        {
            InitializeDB(true);
            long itemID = (long)cbItems.SelectedValue;
            SpendingItems item = spendingItemBl.GetItemByID(itemID);
            SpendingEntries entry = new SpendingEntries()
            {
                SpendingItems = item,
                Date = dtpDate.Value.Date,
                Price = Decimal.Parse(txtEntryPrice.Text),
                Remark = String.IsNullOrEmpty(txtRemarks.Text) ? null : txtRemarks.Text
            };
            spendingEntryBl.Add(entry);
            LoadSpendingEntries();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int userID = (int)cbUsers.SelectedValue;
            IEnumerable<SpendingItems> items = spendingItemBl.GetAllPerUser(userID, txtSearch.Text);
            dgvResults.Rows.Clear();
            foreach (SpendingItems item in items)
            {
                dgvResults.Rows.Add(item.ItemID, item.SpendingCategories.Description, item.Description);
            }
        }

        private void btnAdd2_Click(object sender, EventArgs e)
        {
            if (dgvResults.SelectedRows.Count == 0) return;
            InitializeDB(true);
            long itemID = (long)dgvResults["colItemID", dgvResults.SelectedRows[0].Index].Value; 
            SpendingItems item = spendingItemBl.GetItemByID(itemID);
            SpendingEntries entry = new SpendingEntries()
            {
                SpendingItems = item,
                Date = dtpDate.Value.Date,
                Price = Decimal.Parse(txtEntryPrice2.Text),
                Remark = String.IsNullOrEmpty(txtRemarks2.Text) ? null : txtRemarks2.Text
            };
            spendingEntryBl.Add(entry);
            txtSearch.Text = String.Empty;
            txtEntryPrice2.Text = String.Empty;
            txtRemarks2.Text = String.Empty;
            LoadSpendingEntries();
        }
    }
}
